package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mannortycoon.dao.CropDao;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.entity.SeedEntity;
import com.example.mannortycoon.entity.SeedUnlockConditionsEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.service.SeedService;
import com.example.mannortycoon.service.SeedUnlockConditionsService;
import com.example.mannortycoon.service.UserSeedService;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.Query;
import com.example.mannortycoon.vo.CropVO;
import com.example.mannortycoon.vo.UnlockConditionInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

@Service("cropService")
public class CropServiceImpl extends ServiceImpl<CropDao, CropEntity> implements CropService {

    /**
     * 循环依赖使用@Lazy
     */
    @Autowired
    @Lazy
    private SeedUnlockConditionsService seedUnlockConditionsService;

    @Autowired
    @Lazy
    private SeedService seedService;

    @Autowired
    @Lazy
    private UserSeedService userSeedService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CropEntity> page = this.page(
                new Query<CropEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> asList) {
        //删除所有关联的种子解锁条件
        UpdateWrapper<SeedUnlockConditionsEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("crop_id",asList);
        seedUnlockConditionsService.remove(wrapper);
        //删除作物
        this.removeByIds(asList);
    }

    @Override
    public List<CropVO> getDetailList(Long userId) {
        //获取所有种子
        List<SeedEntity> list = seedService.list();
        return list.stream().map(item->{
            CropVO cropVO = new CropVO();
            //获取作物信息
            CropEntity byId = this.getById(item.getCropId());
            BeanUtils.copyProperties(byId,cropVO);
            cropVO.setIsSold(item.getIsSold());
            //获取种子解锁条件
            List<UnlockConditionInfo> unlockConditionsBySeedId = seedUnlockConditionsService.getUnlockConditionsBySeedId(item.getSeedId());
            cropVO.setUnlockConditions(unlockConditionsBySeedId);
            //获取用户该种子信息
            boolean has = userSeedService.has(userId, item.getSeedId());
            cropVO.setIsUnLock(has ? 1 : 0);
            return cropVO;
        }).collect(Collectors.toList());
    }

}